//
//  iMorseMessengerViewController.h
//  iMorseMessenger

//  Created by xlucansk on 6/8/11.

#import "iMorseMessengerViewController.h"

@implementation iMorseMessengerViewController

@synthesize finalText;
@synthesize finalMorse;
@synthesize String;

NSMutableArray *timers;

-(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event
{
    [finalText resignFirstResponder];
    [super touchesBegan:touches withEvent:event];
}

-(void) changeToWhiteColorLong {
    self.view.backgroundColor = [UIColor whiteColor];
    [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(changeToBlackColor) userInfo:nil repeats:NO];
}

-(void) changeToWhiteColorShort {
     self.view.backgroundColor = [UIColor whiteColor];
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changeToBlackColor) userInfo:nil repeats:NO];
}

-(void) changeToBlackColor {
    self.view.backgroundColor = [UIColor blackColor];
}

//// this changes the color
//-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    [super touchesMoved:touches withEvent:event];
//    self.view.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
//    
//};

-(IBAction) submitButton {
    self.String = finalText.text;
    finalMorse.text = [self textToMorse:[self convertText:self.String]];
    [self morseString:finalMorse.text];
}

-(IBAction) endButton {
    self.view.backgroundColor = [UIColor blackColor];
    for (NSTimer* timer in timers) {
        [timer invalidate];
    }

}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"iMorseMessenger";
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

-(void) morseString: (NSString*) text {
    timers = [[NSMutableArray alloc] init];
    float counter = 0.0;
    
    for (int i = 0; i < text.length; i++) {
		if ([text characterAtIndex:i] == '.') {
                [timers addObject:[NSTimer scheduledTimerWithTimeInterval:counter target:self selector:@selector(changeToWhiteColorShort) userInfo:nil repeats:NO]];
            counter +=1.0;
        }else { 
            if ([text characterAtIndex:i] == '-'){
                [timers addObject: [NSTimer scheduledTimerWithTimeInterval:counter target:self selector:@selector(changeToWhiteColorLong) userInfo:nil repeats:NO]];
                counter +=2.0;
            } else {
                counter +=1.5;
            }
        }
	}
}


//  Created by xvasekov and xdonko.

-(NSString*) convertText: (NSString*) text {
	return [text lowercaseString];
}

-(NSString*) textToMorse: (NSString*) text {
	NSString *morse = @"";
	NSString *formatedText = [self convertText: text];
	//NSLog(@"%@",text);
	for (int i = 0; i < text.length; i++) {
		morse = [NSString stringWithFormat:@"%@ %@", morse, [self charToMorseChar:[formatedText characterAtIndex:i]]];
	}
	//NSLog(@"%@",morse);
	return morse;
}

-(NSString*) charToMorseChar: (char) character {
	
	NSString *morseChar = @"";
	
	switch (character) {
		case ' ':
			morseChar = @" ";
			break;
		case 'a':
			morseChar = @".-";
			break;
		case 'b':
			morseChar = @"-...";
			break;
		case 'c':
			morseChar = @"-.-.";
			break;
		case 'd':
			morseChar = @"-..";
			break;
		case 'e':
			morseChar = @".";
			break;
		case 'f':
			morseChar = @"..-.";
			break;
		case 'g':
			morseChar = @"--.";
			break;
		case 'h':
			morseChar = @"....";
			break;
		case 'i':
			morseChar = @"..";
			break;
		case 'j':
			morseChar = @".---";
			break;
		case 'k':
			morseChar = @"-.-";
			break;
		case 'l':
			morseChar = @".-..";
			break;
		case 'm':
			morseChar = @"--";
			break;
		case 'n':
			morseChar = @"-.";
			break;
		case 'o':
			morseChar = @"---";
			break;
		case 'p':
			morseChar = @".--.";
			break;
		case 'q':
			morseChar = @"--.-";
			break;
		case 'r':
			morseChar = @".-.";
			break;
		case 's':
			morseChar = @"...";
			break;
		case 't':
			morseChar = @"-";
			break;
		case 'u':
			morseChar = @"..-";
			break;
		case 'v':
			morseChar = @"...-";
			break;
		case 'w':
			morseChar = @".--";
			break;
		case 'x':
			morseChar = @"-..-";
			break;
		case 'y':
			morseChar = @"-.--";
			break;
		case 'z':
			morseChar = @"--..";
			break;
		case '0':
			morseChar = @"-----";
			break;
		case '1':
			morseChar = @".----";
			break;
		case '2':
			morseChar = @"..---";
			break;
		case '3':
			morseChar = @"...--";
			break;
		case '4':
			morseChar = @"....-";
			break;
		case '5':
			morseChar = @".....";
			break;
		case '6':
			morseChar = @"-....";
			break;
		case '7':
			morseChar = @"--...";
			break;
		case '8':
			morseChar = @"---..";
			break;
		case '9':
			morseChar = @"----.";
			break;
            
            
		default:
			break;
	}
	return [NSString stringWithFormat:@"%@", morseChar];
}

@end
